<template>
  <view>
    <view v-for="item,i in goodsList" :key="i" @click="gotoDateil(item)">
     <my-goods :goods="item"></my-goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10,
        },
        goodsList: [], //存放商品列表的数据
        total: 0, //总数量,用来实现分页效果
        isLoading:false,//设置节流伐
      };
    },
    onLoad(options) {
      console.log(options)
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      console.log(this.queryObj)

      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        this.isLoading=true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        console.log(res)
        cb && cb() 
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goodsList =[...this.goodsList,...res.message.goods]
        this.total = res.message.total
        this.isLoading=false
      },
      //跳到详情页
      gotoDateil(item){
        uni.navigateTo({
          url:'/subpkg/goods_dateil/goods_dateil?goods_id='+item.goods_id
        })
      }
    },
    // 实现上拉加载更多
    onReachBottom(){
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.$showMsg('数据没有更多数据')
      // 控制节流阀
      if(this.isLoading)return
      // 页码自增+1
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    // 下拉刷新
    onPullDownRefresh(){
      this.isLoading=false;
      this.queryObj.pagenum=1
      this.total=0
      this.goodsList=[]
      this.getGoodsList(()=>uni.stopPullDownRefresh())
    },

  }
</script>

<style lang="scss">
  
</style>
